package com.xiaoyu.izf.controller;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.xiaoyu.izf.aspect.RequestAuth;
import com.xiaoyu.izf.model.MerchantPayChannel;
import com.xiaoyu.izf.model.MerchantWxPayChannelInfo;
import com.xiaoyu.izf.model.MerchantZfbPayChannelInfo;
import com.xiaoyu.izf.model.page.DataTableBean;
import com.xiaoyu.izf.model.page.FormResultBean;
import com.xiaoyu.izf.model.page.ParamListBean;
import com.xiaoyu.izf.service.MerchantPayChannelService;
import com.xiaoyu.izf.service.MerchantWxPayChannelInfoService;
import com.xiaoyu.izf.service.MerchantZfbPayChannelInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Controller
@RequestMapping("/merchant/pay/channel")
public class MerchantPayChannelController extends BaseController {

    @Autowired
    private MerchantPayChannelService merchantPayChannelService;
    @Autowired
    private MerchantZfbPayChannelInfoService merchantZfbPayChannelInfoService;
    @Autowired
    private MerchantWxPayChannelInfoService merchantWxPayChannelInfoService;

    @RequestMapping("/admin_list_page")
    @RequestAuth
    public String adminListPage(HttpServletRequest request, Model model,int merchantId){
        model.addAttribute("merchantId",merchantId);
        return "mgr/pay_channel/admin_list";
    }
    @RequestMapping("/admin_list")
    @ResponseBody
    @RequestAuth
    public DataTableBean adminList(HttpServletRequest request, @RequestBody String body) throws IOException {
        ParamListBean paramListBean = getParamListBean(body);
        DataTableBean dataTableBean = merchantPayChannelService.list(paramListBean);
        return dataTableBean;
    }

    @RequestMapping("/par_list_page")
    @RequestAuth
    public String parListPage(HttpServletRequest request, Model model,int merchantId){
        model.addAttribute("merchantId",merchantId);
        return "mgr/pay_channel/par_list";
    }
    @RequestMapping("/par_list")
    @ResponseBody
    @RequestAuth
    public DataTableBean parList(HttpServletRequest request, @RequestBody String body) throws IOException {
        ParamListBean paramListBean = getParamListBean(body);
        DataTableBean dataTableBean = merchantPayChannelService.list(paramListBean);
        return dataTableBean;
    }

    /**
     * 申请开通支付通道
     * @param request
     * @param model
     * @param payChannelId
     * @return
     */
    @RequestMapping("/apply_open_page")
    @RequestAuth
    public String applyOpenPage(HttpServletRequest request, Model model,int payChannelId){
        model.addAttribute("payChannelId",payChannelId);
        MerchantPayChannel channel = merchantPayChannelService.findOneById(payChannelId);
        if (channel.getType()==1){
            return "mgr/pay_channel/apply_open_zfb";
        }else if (channel.getType()==2){
            return "mgr/pay_channel/apply_open_wx";
        }
        return "";
    }

    @RequestMapping("/apply_open_zfb")
    @ResponseBody
    @RequestAuth()
    public FormResultBean applyOpenZfb(HttpServletRequest request, int payChannelId, MerchantZfbPayChannelInfo zi) {
        FormResultBean fr = new FormResultBean();

        MerchantPayChannel payChannel = merchantPayChannelService.findOneById(payChannelId);
        if (payChannel==null){
            return fr.setType("error").setMessage("支付方式不存在。");
        }

        if (zi.getSettleType()==2){
            if (!Validator.isMobile(zi.getSettlemanPhone())){
                return fr.setType("error").setMessage("手机号格式错误。");
            }
            if (!Validator.isCitizenId(zi.getSettlemanIdcardnumber())){
                return fr.setType("error").setMessage("身份证号码格式不正确。");
            }
        }

        if (StrUtil.isBlank(zi.getPicBankcard())||StrUtil.isBlank(zi.getPicBusinessLicence())||StrUtil.isBlank(zi.getPicCashier())||
                StrUtil.isBlank(zi.getPicDoorhead())||StrUtil.isBlank(zi.getPicIdcardInhand())||StrUtil.isBlank(zi.getPicLeaseContract())||

                StrUtil.isBlank(zi.getPicLegalpersonIdcardFront())||StrUtil.isBlank(zi.getPicLegalpersonIdcardReverse())||StrUtil.isBlank(zi.getPicManageLocation())||
                StrUtil.isBlank(zi.getPicOpenAccountLicence())||
                StrUtil.isBlank(zi.getPicSettlemanIdcardFront())||StrUtil.isBlank(zi.getPicSettlemanIdcardReverse())||StrUtil.isBlank(zi.getPicUnlegalpersonPrivateAuthletter())){
            return fr.setType("error").setMessage("请上传对应资质图片。");
        }

        merchantZfbPayChannelInfoService.save(payChannel,request);

        return fr.setType("success").setMessage("操作成功。");
    }

    @RequestMapping("/apply_open_wx")
    @ResponseBody
    @RequestAuth()
    public FormResultBean applyOpenWx(HttpServletRequest request, int payChannelId, MerchantWxPayChannelInfo wi) {
        FormResultBean fr = new FormResultBean();

        MerchantPayChannel payChannel = merchantPayChannelService.findOneById(payChannelId);
        if (payChannel==null){
            return fr.setType("error").setMessage("支付通道不存在。");
        }

        if (wi.getSettleType()==2){
            if (!Validator.isMobile(wi.getSettlemanPhone())){
                return fr.setType("error").setMessage("手机号格式错误。");
            }
            if (!Validator.isCitizenId(wi.getSettlemanIdcardnumber())){
                return fr.setType("error").setMessage("身份证号码格式不正确。");
            }
        }

        if (StrUtil.isBlank(wi.getPicBankcard())||StrUtil.isBlank(wi.getPicBusinessLicence())||StrUtil.isBlank(wi.getPicCashier())||
                StrUtil.isBlank(wi.getPicDoorhead())||StrUtil.isBlank(wi.getPicIdcardInhand())||StrUtil.isBlank(wi.getPicLeaseContract())||
                StrUtil.isBlank(wi.getPicLegalpersonIdcardFront())||StrUtil.isBlank(wi.getPicLegalpersonIdcardReverse())||StrUtil.isBlank(wi.getPicManageLocation())||
                StrUtil.isBlank(wi.getPicOpenAccountLicence())||
                StrUtil.isBlank(wi.getPicSettlemanIdcardFront())||StrUtil.isBlank(wi.getPicSettlemanIdcardReverse())||StrUtil.isBlank(wi.getPicUnlegalpersonPrivateAuthletter())){
            return fr.setType("error").setMessage("请上传对应资质图片。");
        }

        merchantWxPayChannelInfoService.save(payChannel,request);

        return fr.setType("success").setMessage("操作成功。");
    }

    /**
     * 管理员审核支付通道
     * @param request
     * @param model
     * @param payChannelId
     * @return
     */
    @RequestMapping("/check_pay_channel_page")
    @RequestAuth
    public String checkPayChannelPage(HttpServletRequest request, Model model,int payChannelId){
        MerchantPayChannel channel = merchantPayChannelService.findOneById(payChannelId);
        model.addAttribute("payChannelId",payChannelId);
        model.addAttribute("payChannel",channel);
        if (channel.getType()==1){
            model.addAttribute("zi",merchantZfbPayChannelInfoService.findOne(channel.getAssociatedId()));
            return "mgr/pay_channel/check_pay_channel_zfb";
        }else if (channel.getType()==2){
            model.addAttribute("wi",merchantWxPayChannelInfoService.findOne(channel.getAssociatedId()));
            return "mgr/pay_channel/check_pay_channel_wx";
        }
        return "";
    }
    @RequestMapping("/check_pay_channel")
    @ResponseBody
    @RequestAuth()
    public FormResultBean checkPayChannel(HttpServletRequest request, int payChannelId,int opened) {
        System.err.println(payChannelId);
        System.err.println(opened);
        FormResultBean fr = new FormResultBean();
        MerchantPayChannel payChannel = merchantPayChannelService.findOneById(payChannelId);
        if (payChannel==null){
            return fr.setType("error").setMessage("支付通道不存在。");
        }
        merchantPayChannelService.setOpened(payChannel,opened);
        return fr.setType("success").setMessage("操作成功。");
    }

}
